<script>
  import api from '@/services/api'
  import { ref } from 'vue'
  import { useStore } from 'vuex'
  import { useRouter, useRoute } from 'vue-router'

  export default {
    name: 'LoginView',
    setup() {
      const store = useStore()
      const router = useRouter()
      const route = useRoute()
      const username = ref('')
      const password = ref('')
      const errorMessage = ref('')

      const handleLogin = async () => {
        errorMessage.value = ''
        try {
          const res = await api.post('/auth/login', {
            UserName: username.value,
            Password: password.value
          })
          if (res.code === 0) {
            localStorage.setItem('token', res.data.token)
            localStorage.setItem('user', JSON.stringify(res.data.user))
            await store.dispatch('auth/login', { token: res.data.token, user: res.data.user })
            const redirectPath = route.query.redirect || '/'
            router.push(redirectPath)
          } else {
            errorMessage.value = res.message || '登录失败，请检查账号或密码'
          }
        } catch (error) {
          console.error('Login failed:', error)
          errorMessage.value = '登录失败，请检查网络或稍后重试'
        }
      }

      return {
        username,
        password,
        errorMessage,
        handleLogin,
      }
    },
  }
</script>

<template>
  <div class="login-page-wrapper">
    <div class="login-form-section">
      <div class="login-container">
        <div class="login-header">
          <h2>欢迎登录</h2>
          <p>享受您的专属音乐世界</p>
        </div>
        <form @submit.prevent="handleLogin" class="login-form">
          <div v-if="errorMessage" class="error-message">{{ errorMessage }}</div>
          <div class="input-group">
            <label for="username">账号</label>
            <input type="text" id="username" v-model="username" placeholder="请输入您的账号" required />
          </div>
          <div class="input-group">
            <label for="password">密码</label>
            <input type="password" id="password" v-model="password" placeholder="请输入您的密码" required />
          </div>
          <div class="form-actions">
            <router-link to="/forgot-password" class="link-text">忘记密码？</router-link>
            <router-link to="/register" class="link-text">注册新账号</router-link>
          </div>
          <button type="submit" class="login-button">登录</button>
        </form>
      </div>
    </div>

    <div class="music-visualizer-section">
      <div class="music-visualizer">
        <!-- TODO: 需要替换为后端数据 - 动态音乐封面
             建议接口: GET /api/music/trending-covers (获取热门或推荐的专辑封面)
             响应体: Array<string> (图片URL数组)
        -->
        <div class="cover-item cover-1"></div>
        <div class="cover-item cover-2"></div>
        <div class="cover-item cover-3"></div>
        <div class="cover-item cover-4"></div>
        <div class="cover-item cover-5"></div>
        <div class="cover-item cover-6"></div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.login-page-wrapper {
  display: flex;
  min-height: 100vh;
  width: 100%;
  background: #1a1a1a;
  font-family: 'Arial', sans-serif;
  overflow: hidden;
  /* border: 2px solid red; */ /* 移除诊断性边框 */
}

.login-form-section {
  flex: 0 0 50%;
  box-sizing: border-box;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 2rem;
  z-index: 10;
  position: relative;
  background: #1a1a1a;
  min-width: 0;
  /* border: 2px solid red; */ /* 移除诊断性边框 */
}

/* 动态背景元素 (仅限于左侧) */
.login-form-section::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: rgba(255, 255, 255, 0.03);
  transform: rotate(45deg);
  animation: rotateBg 20s linear infinite;
}

@keyframes rotateBg {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.login-container {
  background: rgba(28, 28, 28, 0.9);
  padding: 40px;
  border-radius: 15px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
  text-align: center;
  width: 100%;
  max-width: 450px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(5px);
}

.login-header h2 {
  color: var(--primary-color);
  font-size: 2.5em;
  margin-bottom: 10px;
  letter-spacing: 2px;
}

.login-header p {
  color: #a0a0a0;
  font-size: 1.1em;
  margin-bottom: 30px;
}

.error-message {
  color: #ff6b6b;
  background-color: rgba(255, 0, 0, 0.1);
  border: 1px solid #ff6b6b;
  padding: 10px;
  border-radius: 8px;
  margin-bottom: 20px;
  font-size: 0.95em;
}

.input-group {
  margin-bottom: 25px;
  text-align: left;
}

.input-group label {
  display: block;
  color: #fff;
  font-size: 0.9em;
  margin-bottom: 8px;
  font-weight: bold;
}

.input-group input {
  width: 100%;
  padding: 12px 15px;
  border: 1px solid #444;
  border-radius: 8px;
  background-color: #333;
  color: #fff;
  font-size: 1.1em;
  transition: all 0.3s ease;
}

.input-group input::placeholder {
  color: #888;
}

.input-group input:focus {
  border-color: #45f3ff;
  box-shadow: 0 0 10px rgba(69, 243, 255, 0.5);
  outline: none;
}

.form-actions {
  display: flex;
  justify-content: space-between;
  margin-top: -10px;
  margin-bottom: 30px;
}

.link-text {
  color: #a0a0a0;
  text-decoration: none;
  font-size: 0.9em;
  transition: color 0.3s ease;
}

.link-text:hover {
  color: #45f3ff;
}

.login-button {
  width: 100%;
  padding: 15px;
  background-color: var(--primary-color);
  color: var(--text-white);
  border: none;
  border-radius: 8px;
  font-size: 1.2em;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 5px 15px rgba(var(--primary-color-rgb), 0.4);
}

.login-button:hover {
  background-color: var(--primary-color-dark);
  transform: translateY(-3px);
  box-shadow: 0 8px 20px rgba(var(--primary-color-rgb), 0.6);
}

/* Music Visualizer Section */
.music-visualizer-section {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #282828;
  overflow: hidden;
  position: relative;
  min-width: 0;
  /* border: 2px solid red; */ /* 移除诊断性边框 */
}

.music-visualizer {
  position: relative;
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(2, 1fr);
  gap: 1.5rem;
  padding: 1.5rem;
  box-sizing: border-box;
  /* border: 2px solid red; */ /* 移除诊断性边框 */
}

.cover-item {
  width: 100%;
  padding-top: 100%;
  background-size: cover;
  background-position: center;
  border-radius: 10px;
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.6); /* 增强阴影以增加深度 */
  /* animation: coverAnimate 20s infinite ease-in-out alternate; */ /* 动画持续时间单独设置 */
  opacity: 0.9;
  transform-origin: center center -50px; /* 设置变换的原点在Z轴上 */
  backface-visibility: hidden; /* 确保背面不渲染，增强3D效果 */
}

/* Example covers - Replace with actual images or dynamically loaded ones */
.cover-1 { background-image: url('/music_covers/cover1.jpg'); animation: coverAnimate 18s infinite ease-in-out alternate; animation-delay: 0s; transform-origin: center center -60px; }
.cover-2 { background-image: url('/music_covers/cover2.jpg'); animation: coverAnimate 20s infinite ease-in-out alternate; animation-delay: 1.5s; transform-origin: center center -40px; }
.cover-3 { background-image: url('/music_covers/cover3.jpg'); animation: coverAnimate 19s infinite ease-in-out alternate; animation-delay: 3s; transform-origin: center center -70px; }
.cover-4 { background-image: url('/music_covers/cover4.jpg'); animation: coverAnimate 21s infinite ease-in-out alternate; animation-delay: 4.5s; transform-origin: center center -30px; }
.cover-5 { background-image: url('/music_covers/cover5.jpg'); animation: coverAnimate 17s infinite ease-in-out alternate; animation-delay: 6s; transform-origin: center center -80px; }
.cover-6 { background-image: url('/music_covers/cover6.jpg'); animation: coverAnimate 22s infinite ease-in-out alternate; animation-delay: 7.5s; transform-origin: center center -20px; }

@keyframes coverAnimate {
  0% {
    transform: scale(0.95) translate3d(5px, 5px, -50px) rotateX(2deg) rotateY(-2deg) rotateZ(1deg); /* 更小的位移和旋转幅度 */
    opacity: 0.1; /* 保持不透明度低，增强渐近效果 */
  }
  50% {
    transform: scale(1.02) translate3d(-5px, -5px, 25px) rotateX(-2deg) rotateY(2deg) rotateZ(-1deg); /* 调整缩放和位移、旋转 */
    opacity: 1;
  }
  100% {
    transform: scale(0.95) translate3d(5px, 5px, -50px) rotateX(2deg) rotateY(-2deg) rotateZ(1deg); /* 循环回到初始状态 */
    opacity: 0.1; /* 保持不透明度低，增强渐近效果 */
  }
}

/* Responsive adjustments */
@media (max-width: 900px) {
  .login-page-wrapper {
    flex-direction: column;
  }

  .music-visualizer-section {
    /* display: none; */
  }

  .login-form-section {
    width: 100%;
    padding: 1rem;
  }
}

@media (max-width: 600px) {
  .login-container {
    padding: 30px;
  }

  .login-header h2 {
    font-size: 2em;
  }

  .login-header p {
    font-size: 1em;
  }

  .input-group label,
  .link-text {
    font-size: 0.8em;
  }

  .input-group input {
    padding: 10px 12px;
    font-size: 1em;
  }

  .login-button {
    padding: 12px;
    font-size: 1.1em;
  }
}
</style>
